All editions of Visual Basic come with the Common Dialog Control. This control allows you to display common dialog boxes like Open, Set Colour, and Print, simply by setting a few properties. To add the Common Dialog Control to your project click Project | Components. Then check the box next to Microsoft Common Dialog Control and clikc OK. You will now see a new icon in your toolbox.

To show one of the dialog boxes contained in the Common Dialog control, you need to use the following syntax:

CommonDialog1.DialogBox

Where DialogBox is one of the following methods (the methods in italic are not covered in this article):

Method Action 
ShowSave Displays the Save Dialog 
ShowOpen Displays the Open Dialog 
ShowPrinter Displays the Print Dialog 
ShowColor Displays the Colour Dialog 
ShowHelp Displays the Help Dialog 
ShowFont Displays the Font Dialog 

As you can see, it is very easy to user the Common Dialog Control. However, in order to use the control to its full potential, you need to set a few parameters before showing the dialog. These parameters will be different, depending on which dialog you want to show. The next few sections describe them.

You can use the open dialog to allow the user to select a file, which your application can then open, or analyse etc. Below are the most common parameters you will need to set before displaying the Open Dialog Box.

Parameter Changes... 
DialogTitle the Title displayed in the Dialog Box 
Filter the File Types displayed in the file type list. 
FilterIndex the default file type 
Flags custom settings such as hide read-only files 
CancelError whether an error occurs when the dialog box is cancelled. 

The following code displays an open Dialog, with read-only files hidden. The extension list is set to allow Word Documents (*.doc) and Excel Spreadsheets (*.xls). The dialog will not allow you to select files that do not exist.

' Sets the Dialog Title to Open File
CommonDialog1.DialogTitle = "Open File"

' Sets the File List box to Word documents and Excel documents
CommonDialog1.Filter = "Word Documents (*.doc)|*.doc|Excel Spreadsheets (*.xls)|*.xls"

' Set the default files type to Word Documents
CommonDialog1.FilterIndex = 1

' Sets the flags - File must exist and Hide Read only
CommonDialog1.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly

' Set dialog box so an error occurs if the dialogbox is cancelled
CommonDialog1.CancelError = True

' Enables error handling to catch cancel error
On Error Resume Next
' display the dialog box
CommonDialog1.ShowOpen
If Err Then
    ' This code runs if the dialog was cancelled
    Msgbox "Dialog Cancelled"
    Exit Sub
End If
' Displays a message box.
Msgbox "You selected " & CommonDialog1.FileName

The Filter property is a string using the following format:

"FileTypeText1 |FileTypeExtension1 | FileTypeText2 | FileTypeExtension2"

The FileName property stores the selected file's path and name. The FileTitle property stores just the filename, you could use this to set a dialogbox's title:

Form1.Caption = "Text From : " & CommonDialog1.FileTitle

The following code displays an save Dialog. The extension list is set to allow Text File (*.txt) and All Files (*.*). It hides readonly files, prompts if you want to overwrite the existinf files, and only allows the file to be saved in a folder that already exists

' Sets the Dialog Title to Save File
CommonDialog1.DialogTitle = "Save File"

' Sets the File List box to Text File and All Files
CommonDialog1.Filter = "Text File (*.txt)|*."|AllFiles (*.*)|*.*"

' Set the default files type to Text File
CommonDialog1.FilterIndex = 1

' Sets the flags - Hide Read only, prompt to overwrite, and path must exist
CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt _
+ cdlOFNPathMustExist

' Set dialog box so an error occurs if the dialogbox is cancelled
CommonDialog1.CancelError = True

' Enables error handling to catch cancel error
On Error Resume Next
' display the dialog box
CommonDialog1.ShowSave
If Err Then
    ' This code runs if the dialog was cancelled
    Msgbox "Dialog Cancelled"
    Exit Sub
End If

' Displays a message box.
Msgbox "You saved the files as  " & CommonDialog1.FileName

If you just want to display the open and save dialogs, what's the point of having a control with extra code you don't need? VB Web has the solution! Take a look at our Open/Save Dlg Control (with multi-file select support)

You can use the print dialog to allow the user to select printer settings, which your application can then use to print. Below are the most common parameters you will need to set before displaying the Print Dialog Box.

Parameter Changes... 
DialogTitle the Title displayed in the Dialog Box 
Printer Default whether to use the default printer 
Flags custom settings such as print selection only 
CancelError whether an error occurs when the dialog box is cancelled. 

The following code displays a print dialog, with page numbers disabled, and then prints the text in RichTextBox1. It also returns the selected printer

CommonDialog1.PrinterDefault = True
CommonDialog1.CancelError = True
' Set flags - no page numbers, return the selected printer
CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums
If RichTextBox1.SelLength = 0 Then
    CommonDialog1.Flags = CommonDialog1.Flags + cdlPDAllPages
Else
    CommonDialog1.Flags = CommonDialog1.Flags + cdlPDSelection
End If
' Enables error handling to catch cancel error
On Error Resume Next
' display the print dialog box
CommonDialog1.ShowPrinter

If Err Then
    ' This code runs if the dialog was cancelled
    Msgbox "Dialog Cancelled"
    Exit Sub
End If
' Prints the contents of RichTextBox
RichTextBox1.SelPrint (Printer.hDC)

You can use the set colour dialog to allow the user to select a system color. Below are the most common parameters you will need to set before displaying the Open Dialog Box.

Parameter Changes... 
DialogTitle the Title displayed in the Dialog Box 
Color the selected color 
Flags custom settings such as disable custom color button. 
CancelError whether an error occurs when the dialog box is cancelled. 

The following code displays a colour dialog. When the user clicks OK, it sets the forms background colour to the one selected.

' Sets the Dialog Title to Save File
CommonDialog1.DialogTitle = &quot;Select Colour&quot;

CommonDialog1.CancelError = True
' Set flags - enabled the Custom Color button
CommonDialog1.Flags = cdlCCFullOpen
' Enables error handling to catch cancel error
On Error Resume Next
' display the set colour dialog box
CommonDialog1.ShowColor

If Err Then
&nbsp;&nbsp;&nbsp; ' This code runs if the dialog was cancelled
&nbsp;&nbsp;&nbsp; Msgbox &quot;Dialog Cancelled&quot;
&nbsp;&nbsp;&nbsp; Exit Sub
End If
' Sets Form1 background color to the selected colour
Form1.Backcolor = CommonDialog1.Color